package com.yznote.cloudesk.user.server.impl;

import com.yznote.cloudesk.common.constants.YConstant;
import com.yznote.cloudesk.common.pojo.DeskFile;
import com.yznote.cloudesk.common.pojo.File;
import com.yznote.cloudesk.common.pojo.Personality;
import com.yznote.cloudesk.common.pojo.User;
import com.yznote.cloudesk.common.utils.IPUtils;
import com.yznote.cloudesk.common.utils.IdUtils;
import com.yznote.cloudesk.common.oss.OssUtils;
import com.yznote.cloudesk.config.JWTconfig;
import com.yznote.cloudesk.file.mapper.DeskMapper;
import com.yznote.cloudesk.file.mapper.FileMapper;
import com.yznote.cloudesk.file.server.FileServer;
import com.yznote.cloudesk.user.mapper.BarMapper;
import com.yznote.cloudesk.user.mapper.PersonalityMapper;
import com.yznote.cloudesk.user.mapper.UserMapper;
import com.yznote.cloudesk.user.server.LoginServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.time.LocalDateTime;

/**
 * @author : xiaozhoutongxue
 * @date : 2022/10/15 17:03
 */
@Slf4j
@Service
public class LoginServerImpl implements LoginServer {
    @Autowired
    UserMapper userMapper;
    @Autowired
    DeskMapper deskMapper;
    @Autowired
    FileMapper fileMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    FileServer fileServer;
    @Autowired
    OssUtils ossUtils;
    @Autowired
    PersonalityMapper personalityMapper;
    @Autowired
    BarMapper barMapper;

    @Override
    public String login(User reUser) {
        User user = userMapper.login(reUser.getUserName(), reUser.getPassword());
        if (user == null) {
            return YConstant.ERROR_USERORPWD;
        }
        if (user.getStatus()==1){
            return YConstant.ERROR_STATUSEXC;
        }
        String requestIp = IPUtils.getRequestIp();
        String token = JWTconfig.createToken(user.getUserName(), user.getUserId());
        userMapper.setLoginTime(user.getUserId(), requestIp, LocalDateTime.now());
        return token;
    }

    @Transactional
    @Override
    public String register(User user) throws UnsupportedEncodingException {
        Long userId = IdUtils.nextId();
        String msg = IdUtils.verifyUser(user);
        LocalDateTime now =LocalDateTime.now();
        if (msg!=YConstant.SUCCESS){
            return msg;
        }
        user.setUserId(userId);
        user.setCreateBy(userId);
        user.setCreateTime(now);
        if (userMapper.havaUserName(user.getUserName())!=0){
            return YConstant.ERROR_USERREPEAT;
        }
        userMapper.register(user);
        //分配我的电脑
        Long myComFileId = IdUtils.nextId();
        File file =new File();
        file.setFileName("我的电脑");
        file.setFileId(myComFileId);
        file.setFileType(0L);
        file.setFileFaId(-1L);
        file.setFileLevel(0);
        file.setUserId(userId);
        file.setFileIcon("computer.jpeg");
        file.setCreateBy(userId);
        file.setCreateTime(now);
        fileMapper.insertFilemsg(file);
        deskMapper.insertFile(new DeskFile(userId,myComFileId,0,0));

        //分配我的桌面
        File file1 =new File();
        file1.setFileName("我的桌面");
        file1.setFileId(userId);
        file1.setUserId(userId);
        file1.setFileType(0L);
        file1.setFileFaId(myComFileId);
        file1.setFileLevel(1);
        file1.setFileIcon("desktop.jpeg");
        file1.setCreateBy(userId);
        file1.setCreateTime(now);
        fileMapper.insertFilemsg(file1);

        //分配回收站
        Long garbegeFileId = IdUtils.nextId();
        File file2 =new File();
        file2.setFileName("回收站");
        file2.setFileId(garbegeFileId);
        file2.setFileType(9L);
        file2.setFileFaId(userId);
        file2.setFileLevel(2);
        file2.setUserId(userId);
        file2.setFileIcon("buket.jpeg");
        file2.setCreateBy(userId);
        file2.setCreateTime(now);
        fileMapper.insertFilemsg(file2);
        deskMapper.insertFile(new DeskFile(userId,garbegeFileId,0,1));
        //分配默认头像和背景
        Personality personality =new Personality();
        personality.setUserId(userId);
        //默认路径
        personality.setUserHeadsPath("icons/heads1.webp");
        personality.setUserWallpaperPath(null);
        personality.setCreateBy(userId);
        personality.setCreateTime(now);
        personalityMapper.insertPersonality(personality);
        //设置下边状态栏
        barMapper.insetBar(userId,myComFileId,0,now);
        return YConstant.SUCCESS;
    }

    @Override
    public Boolean updataUser(User user) {
        return null;
    }

}
